import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, Index } from 'typeorm';

// 消息状态枚举
export enum MessageStatus {
  UNREAD = 0,     // 未读
  READ = 1,       // 已读
  DELETED = 2     // 已删除
}

// 消息类型枚举
export enum MessageType {
  SYSTEM = 0,     // 系统消息
  ADMISSION = 1,  // 录取消息
  PAYMENT = 2,    // 缴费消息
  OTHER = 3       // 其他消息
}

@Entity('system_messages')
export class SystemMessage {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

  @Column({ type: 'varchar', length: 100, comment: '消息标题' })
  title!: string;

  @Column({ type: 'text', comment: '消息内容' })
  content!: string;

  @Column({ type: 'varchar', length: 255, nullable: true, comment: '链接URL' })
  url?: string;

  @Column({ 
    type: 'tinyint',
    default: MessageType.SYSTEM,
    comment: '消息类型：0 系统消息 1 录取消息 2 缴费消息 3 其他'
  })
  type!: MessageType;

  @Column({ 
    type: 'tinyint',
    default: MessageStatus.UNREAD,
    comment: '消息状态：0 未读 1 已读 2 已删除'
  })
  status!: MessageStatus;

  @Index('idx_user_id')
  @Column({ type: 'varchar', length: 20, name: 'user_id', comment: '用户ID' })
  userId!: string;

  @CreateDateColumn({ 
    type: 'timestamp',
    name: 'create_time',
    comment: '创建时间'
  })
  createTime!: Date;

  @UpdateDateColumn({ 
    type: 'timestamp',
    name: 'update_time',
    comment: '修改时间'
  })
  updateTime!: Date;

  @Column({ type: 'varchar', length: 50, nullable: true, name: 'created_by', comment: '创建人' })
  createdBy?: string;

  constructor(data?: Partial<SystemMessage>) {
    if (data) {
      Object.assign(this, data);
    }
  }

  get typeText(): string {
    const typeMap: { [key in MessageType]: string } = {
      [MessageType.SYSTEM]: '系统消息',
      [MessageType.ADMISSION]: '录取消息',
      [MessageType.PAYMENT]: '缴费消息',
      [MessageType.OTHER]: '其他消息'
    };
    return typeMap[this.type];
  }

  get statusText(): string {
    const statusMap: { [key in MessageStatus]: string } = {
      [MessageStatus.UNREAD]: '未读',
      [MessageStatus.READ]: '已读',
      [MessageStatus.DELETED]: '已删除'
    };
    return statusMap[this.status];
  }
} 